#include "CSaveLoadState.h"
#include "CMainMenuState.h"
#include "CGame.h"
#include "CPrintBitmapToScreen.h"
#include "Wrappers/tinyxml.h"
#include "CCyberSpaceState.h"

CSaveLoadState::CSaveLoadState(void)
{
	m_nCurrentSelection = 0;
	m_nCursorID = -1;
	m_nBackgroundImageID = -1;
}

CSaveLoadState::CSaveLoadState(const CSaveLoadState&)
{

}

CSaveLoadState::~CSaveLoadState(void)
{

}

CSaveLoadState& CSaveLoadState::operator=(const CSaveLoadState&)
{
	return *this;
}

CSaveLoadState* CSaveLoadState::GetInstance()
{
	static CSaveLoadState slState;
	return &slState;
}

bool CSaveLoadState::Save(const char* szXmlFileName)
{
	TiXmlDocument doc;
	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0", "utf-8", "");


	TiXmlElement* pRoot = new TiXmlElement("levelState");

	// Save the current room
	TiXmlElement* pRoom = new TiXmlElement("currRoom");
	pRoom->SetAttribute("roomName", SNACK);
	pRoot->LinkEndChild(pRoom);

	TiXmlElement* pText = new TiXmlElement("Player1");
	if(m_nCurrentSelection == 0)
		pText->SetValue(m_cNameOne);
	else if(m_nCurrentSelection == 1)
		pText->SetValue(m_cNameTwo);
	else if(m_nCurrentSelection == 2)
		pText->SetValue(m_cNameThree);
	pRoot->LinkEndChild(pText);

	doc.LinkEndChild(pDec);
	doc.LinkEndChild(pRoot);

	if(doc.SaveFile(szXmlFileName) == false)
	{
		return false;
	}

	return true;
}

bool CSaveLoadState::Load(const char* szXmlFileName)
{
	TiXmlDocument doc;
	if(doc.LoadFile(szXmlFileName) == false)
	{
		return false;
	}

	TiXmlElement* pRoot = doc.RootElement();

	if(!pRoot)
		return false;

	// Load file
	m_nCurrRoom = atoi(pRoot->FirstChildElement("currRoom")->Attribute("roomName"));
	//string sName = pRoot->FirstChildElement("Player1")->Value();

	CCyberSpaceState::GetInstance()->SetCurrentRoom(m_nCurrRoom);

	return true;
}

void CSaveLoadState::Enter(void)
{
	m_bLoaded = false;

	// Load sounds and textures
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Cursor.png");
}

bool CSaveLoadState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(m_nCurrentSelection > 0)
			--m_nCurrentSelection;
		else
			m_nCurrentSelection = 2;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(m_nCurrentSelection < 2)
			++m_nCurrentSelection;
		else
			m_nCurrentSelection = 0;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT))
	{
		m_nCurrentSelection = 0;
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT))
	{
		m_nCurrentSelection = 3;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(0))
	{
		if(m_nCurrentSelection == 0)
		{
			// Load or save slot 1
			if(GetCurrState() == LOAD)
				Load("SaveOne.xml");
			else if(GetCurrState() == SAVE)
				Save("SaveOne.xml");

			m_bLoaded = true;
			CGame::GetInstance()->PopState();
		}
		else if(m_nCurrentSelection == 1)
		{
			// Load or save slot 2
			if(GetCurrState() == LOAD)
				Load("SaveTwo.xml");
			else if(GetCurrState() == SAVE)
				Save("SaveTwo.xml");

			m_bLoaded = true;
			CGame::GetInstance()->PopState();
		}
		else if(m_nCurrentSelection == 2)
		{
			// Load or save slot 3
			if(GetCurrState() == LOAD)
				Load("SaveThree.xml");
			else if(GetCurrState() == SAVE)
				Save("SaveThree.xml");

			m_bLoaded = true;
			CGame::GetInstance()->PopState();
		}
		else if(m_nCurrentSelection == 3)
		{
			CGame::GetInstance()->PopState();
		}
	}

	
	if(GetCurrState() == SAVE) // If we are saving, allow user to enter name for save slot
	{
		if(m_nCurrentSelection == 0)
		{
			if(CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx() != 0)
			{
				char newChar;
				newChar = CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx();
				if(newChar == 8)
				{
					if(m_vSaveOneName.size() >= 1)
						m_vSaveOneName.pop_back();

					// Create temporary array
					
				}
				else
					m_vSaveOneName.push_back(newChar);

				for(unsigned int i=0; i<m_vSaveOneName.size(); ++i)
				{
					m_cNameOne[i] = m_vSaveOneName[i];
				}
			}
		}
		else if(m_nCurrentSelection == 1)
		{
			if(CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx() != 0)
			{
				char newChar;
				newChar = CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx();
				m_vSaveTwoName.push_back(newChar);

				for(unsigned int i=0; i<m_vSaveTwoName.size(); ++i)
				{
					m_cNameTwo[i] = m_vSaveTwoName[i]; // Update name array
				}
			}
		}
		else if(m_nCurrentSelection == 2)
		{
			if(CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx() != 0)
			{
				char newChar;
				newChar = CSGD_DirectInput::GetInstance()->CheckBufferedKeysEx();
				m_vSaveThreeName.push_back(newChar);

				for(unsigned int i=0; i<m_vSaveThreeName.size(); ++i)
				{
					m_cNameThree[i] = m_vSaveThreeName[i]; // Update name array
				}
			}
		}
	}
	

	return true;
}

void CSaveLoadState::Update(float fElapsedTime)
{
	
}

void CSaveLoadState::Render(void)
{
	// Define slot rects
	RECT rOne, rTwo, rThree;

	rOne.left = 20;
	rOne.top = 20;
	rOne.right = rOne.left + 500;
	rOne.bottom = rOne.top + 100;

	rTwo.left = 20;
	rTwo.top = 140;
	rTwo.right = rTwo.left + 500;
	rTwo.bottom = rTwo.top + 100;

	rThree.left = 20;
	rThree.top = 260;
	rThree.right = rThree.left + 500;
	rThree.bottom = rThree.top + 100;

	// Display save slots
	if(m_nCurrentSelection == 0)
		CSGD_Direct3D::GetInstance()->DrawRect(rOne, 255, 0, 0);
	else
		CSGD_Direct3D::GetInstance()->DrawRect(rOne, 0, 255, 0);

	if(m_nCurrentSelection == 1)
		CSGD_Direct3D::GetInstance()->DrawRect(rTwo, 255, 0, 0);
	else
		CSGD_Direct3D::GetInstance()->DrawRect(rTwo, 0, 255, 0);

	if(m_nCurrentSelection == 2)
		CSGD_Direct3D::GetInstance()->DrawRect(rThree, 255, 0, 0);
	else
		CSGD_Direct3D::GetInstance()->DrawRect(rThree, 0, 255, 0);

	if(m_nCurrentSelection == 3)
		CPrintBitmapFontToScreen::GetInstance()->Print("BACK", 330, 400, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("BACK", 330, 400, 1.0f, D3DCOLOR_XRGB(0,255,0));

	// Draw the cursor
	if(m_nCurrentSelection != 3)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 20, 60 + m_nCurrentSelection*100);
	}
	else
		CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 300, 400);

	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	// Display save names for each slot
	CSGD_Direct3D::GetInstance()->DrawTextA(m_cNameOne, 300, ((rOne.bottom + rOne.top) / 2), 0, 0, 255);
	CSGD_Direct3D::GetInstance()->DrawTextA(m_cNameTwo, 300, ((rTwo.bottom + rTwo.top) / 2), 0, 0, 255);
	CSGD_Direct3D::GetInstance()->DrawTextA(m_cNameThree, 300, ((rThree.bottom + rThree.top) / 2), 0, 0, 255);
}

void CSaveLoadState::Exit(void)
{

}